/*
 * Copyright (C) 2005 - 2007  Bryan D. Payne (bryan@thepaynes.cc)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * --------------------
 * This file provides a simple example for viewing a kernel address in memory.
 *
 * File: map-addr.c
 *
 * Author(s): Bryan D. Payne (bryan@thepaynes.cc)
 */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
#include <stdio.h>
#include <xenaccess/xenaccess.h>
#include <xenaccess/xa_private.h>

#include <linux/sched.h>
#include <asm/unistd.h>

#include "md5.h"

int SYS_CALL_SIZE = 10;
void **sys_call_table;


void print_sys_call_table();
void checkSys_call_table();


typedef struct {
	char *name; //sys_call_table
	int table_size;
	void **table;	
	unsigned char checksum[16];
	void **original_table;
	
	
} monitored_kernel_table;



int main (int argc, char **argv)
{
    xa_instance_t xai;
    unsigned char *memory = NULL;
    uint32_t offset;
    uint32_t *address;

    /* this is the domain ID that we are looking at */
    uint32_t dom = atoi(argv[1]);
    

    /* initialize the xen access library */
    if (xa_init(dom, &xai) == XA_FAILURE){
        perror("failed to init XenAccess library");
        goto error_exit;
    }
    
    /*List Sys_call_table entries  */
    memory = xa_access_kernel_symbol(&xai,"sys_call_table",&offset);
    //long *sys_call_table = memory+offset;
    //int counter=0;
    //for (counter=0;counter<10;counter++)
    //	printf("sys_call_table[%d] : %x \n" , counter , sys_call_table[counter]);     
    


    //memcpy( sys_call_table , memory + offset  ,   sizeof(sys_call_table)   );
    sys_call_table = (int)(memory + offset);
    
    print_sys_call_table();
    checkSys_call_table();
        
    sleep(10);
    
    print_sys_call_table();
    checkSys_call_table();

error_exit:

    /* sanity check to unmap shared pages */
	if (memory) munmap(memory, xai.page_size);

    /* cleanup any memory associated with the XenAccess instance */
    xa_destroy(&xai);

    return 0;
}


void checkSys_call_table(){
	
	unsigned char result[16];
	librad_md5_calc(result, sys_call_table, SYS_CALL_SIZE);
	printf("md5 of %s is ", "sys_call_table");
	hex_dump(result, 16);
	printf("\n");
	
}

void print_sys_call_table(){

    int counter=0;
    for (counter=0;counter< SYS_CALL_SIZE;counter++){
    	printf("sys_call_table[%d] : %x\n" , counter , (int *)sys_call_table[counter]);
    }
    printf("\n");
}


